package com.shigu.cron;

import com.opentae.data.mall.interfaces.UserInviteInfoMapper;
import com.shigu.account.api.VipManagerService;
import com.shigu.account.enums.VipLevel;
import com.shigu.constant.enums.PageUri;
import com.shigu.constant.enums.VipSetting;
import com.shigu.constant.service.SystemConstantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/*
 * create by  过儿  2019-11-12
 * 自动升级定时
 */
@Component
public class AutoUpgradeTask {
    @Autowired
    SystemConstantService systemConstantService;
    @Autowired
    UserInviteInfoMapper userInviteInfoMapper;

    @Resource(name = "vipManagerServiceImpl")
    VipManagerService vipManagerService;

    /**
     *
     * @description 升级店长
     * @author 郭尔
     * @date 2019-11-12 14:13
     */
    @Scheduled(cron = "0/5 * * * * ?")
    public void ownerAutoUpgrade(){
        Map<String, String> map = systemConstantService.selPageConstant(PageUri.vip.toString());
        Integer promote = Integer.valueOf( map.get(VipSetting.ownerPromoteValue.toString()) );
        Double reward = Double.valueOf(map.get(VipSetting.ownerRewardValue.toString()));
        //获取所有需要升级的人
        List<Long> users = userInviteInfoMapper.getUpgradeUser(promote, reward, VipLevel.OWNER.getValue());
        for (Long id : users) {
            vipManagerService.changeUserLevel(id,VipLevel.OWNER.getValue());
        }
    }


    /**
     *
     * @description 升级博士
     * @author 郭尔
     * @date 2019-11-12 14:13
     */
    @Scheduled(cron ="0/5 * * * * ?")
    public void bossAutoUpgrade(){
        Map<String, String> map = systemConstantService.selPageConstant(PageUri.vip.toString());
        Integer promote = Integer.valueOf( map.get(VipSetting.bossPromoteValue.toString()) );
        Double reward = Double.valueOf(map.get(VipSetting.bossRewardValue.toString()));
        //获取所有需要升级的人
        List<Long> users = userInviteInfoMapper.getUpgradeUser(promote, reward, VipLevel.BOSS.getValue());
        for (Long id : users) {
            vipManagerService.changeUserLevel(id,VipLevel.BOSS.getValue());
        }
    }
}
